from awtools.log import log
from selenium import webdriver
from bs4 import BeautifulSoup
# from selenium.webdriver.support.wait import Wait
# from selenium.webdriver.common.by import by

"""
self.currentNum  当前的进度
self.soup  页面的对象
self.error 错误消息
"""

class formatHtml(object):
    def __new__(cls, htmlPath):
        log('这里是开始运行  new ')
        o = object.__new__(cls)
        return o

    def __init__(self, htmlPath):
        self.htmlPath = htmlPath
        log(htmlPath)
        self.readHtml()
        self.getTitle()
        self.getOption()
        self.getAnswer()

    def readHtml(self):
        self.soup = BeautifulSoup(open(self.htmlPath, encoding='utf-8'), features='html.parser')
        log('读取html内容成功')

    def getTitle(self):
        titleObj = self.soup.find('div', {'id': 'checkbox_testTitle'})
        if not titleObj:
            self.error = '没有找到标题'
            return False
        spanObjs = titleObj.findAll('span')
        self.currentNum = spanObjs[0].text
        log.debug(self.currentNum)
        self.totalNum = spanObjs[1].text
        log.debug(self.totalNum)
        self.typeStr = spanObjs[2].text
        log.debug(self.typeStr)
        self.titleStr = spanObjs[3].text
        log.debug(self.titleStr)

    def getOption(self):
        optionObj = self.soup.find('ul', {'id':'checkbox_Items'}).findAll('li')
        optionArrs = []
        for item in optionObj:
            spanObjs = item.findAll('span')
            dicts = {}
            dicts['option'] = spanObjs[0].text
            dicts['option_str'] = spanObjs[1].text
            log.debug(dicts)
            optionArrs.append(dicts)
        self.options = optionArrs
        log.debug(optionArrs)

    def getAnswer(self):
        answerObj = self.soup.find('div', {'class':'content_detail'})
        self.answer = answerObj.find('span', {'class':'right_color'}).text
        log.debug(self.answer)
        dlObjs = answerObj.findAll('dl')
        self.analyze = dlObjs[2].find('dd').text
        log.debug(self.analyze)

    def getData(self):

        data = {}
        data['title'] = self.titleStr
        data['options'] = self.options
        data['other'] = self.currentNum + self.totalNum +self.typeStr
        return data



